<template>
	<view class="content ">
		<view class="swiper">
			<u-swiper height='350' :list="slides" name="img_url" />
		</view>
		<u-tabs :list="tabsList" bar-width='80' item-width="160" :is-scroll="false" :current="current"
			@change="changeTabs" />
		<view class="wrap u-skeleton">
			<u-row gutter="16">
				<!-- 必须循环为对象 防止组件内部报错 -->
				<u-col span="6" v-for="(item,index) in goods.length !==0 ? goods : [{},{},{},{}]" :key='index'>
				<!-- 商品列表 -->
				 <goods-card :item='item'/>
				</u-col>

			</u-row>
		</view>
		<!--引用组件 骨架屏-->
		<u-skeleton :loading="loading" :animation="true" bgColor="#FFF"></u-skeleton>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				tabsList: [{
						name: '默认'
					}, {
						name: '销量'
					}, {
						name: '推荐',

					},
					{
						name: '最新',

					}
				],
				current: 0,
				slides: [],
				goods: [],
				//分页
				page: 1,
				//是否显示骨架屏
				loading: false
			}
		},
		onLoad() {

			this.getIndex()

		},
		methods: {
			changeTabs(index) {
				this.current = index;
				this.goods = []
				this.page = 1
				this.getIndex()
			},
			//获取首页数据
			async getIndex() {
				this.loading =true
				const params = {
					page: this.page
				}
				if (this.current == 1) {
					params.sales = 1
				} else if (this.current == 2) {
					params.recommend = 1
				} else if (this.current == 3) {
					params.new = 1
				}
			
				const res = await this.$u.api.getIndex(params)
					
				//轮播图
				this.slides = res.slides
				this.loading = false
				//将数据追加到数组中去
				this.goods = [...this.goods, ...res.goods.data]

			},
			onReachBottom() {

				this.page = this.page + 1
				this.getIndex()
			}

		}
	}
</script>

<style lang="scss" scoped>
	.u-tabs {
		margin-top: 20rpx;
	}


</style>
